package cn.itsource.hbc.pet.controller;


import cn.itsource.hbc.org.domain.Employee;
import cn.itsource.hbc.org.domain.Shop;
import cn.itsource.hbc.org.service.IShopService;
import cn.itsource.hbc.pet.domain.Pet;
import cn.itsource.hbc.pet.domain.SearchMasterMsg;
import cn.itsource.hbc.pet.query.PetQuery;
import cn.itsource.hbc.pet.query.SearchMasterMsgQuery;
import cn.itsource.hbc.pet.service.IPetService;
import cn.itsource.hbc.pet.service.ISearchMasterMsgService;
import cn.itsource.utils.AjaxResult;
import cn.itsource.utils.LoginContext;
import cn.itsource.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
   private IPetService petService;

    @Autowired
    private IShopService shopService;

    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody Pet pet,  HttpServletRequest request){
        // 获取当前登录的用户信息
        Employee employee =(Employee) LoginContext.getCurrent(request);
        // 获取对应的店铺
        Shop shop = shopService.loadByAdminId(employee.getId());

        pet.setShop_id(shop.getId());



        try {
            if(pet.getId()!=null){
                petService.update(pet);
            }else {
                petService.save(pet);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();

            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());

        }

    }

    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){

        try {
            petService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败"+ e.getMessage());

        }


    }


    @GetMapping("/{id}")
    public Pet loadById(@PathVariable("id") Long id){

        return petService.loadById(id);
    }
    @GetMapping("/detail/{id}")
    public Pet loadByIdOfFront(@PathVariable("id") Long id){

        return petService.loadByIdOfFront(id);
    }


    @GetMapping
    public List<Pet> loadAll(){

        return petService.loadAll();
    }

    @PostMapping
    public PageResult<Pet> queryPage(@RequestBody PetQuery query , HttpServletRequest request){
        // 获取当前登录的用户信息
        Employee employee =(Employee) LoginContext.getCurrent(request);
        // 获取对应的店铺
        Shop shop = shopService.loadByAdminId(employee.getId());
        // 设置shopid到查询中
        query.setShopId(shop.getId());


        return petService.queryPage(query);
    }
    @PostMapping("/front")
    public PageResult<Pet> queryPageOfFront(@RequestBody PetQuery query ){


        return petService.queryPage(query);
    }


    @PatchMapping
    public AjaxResult allDelete(@RequestBody Long[] ids){
        // 传回来的数据是id的一个数组，将业务交给service'
        try {
            petService.allDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage()+"操作失败");
        }

    }

    @PatchMapping("/onSale")
    public AjaxResult onSale(@RequestBody Long[] ids){
        // 传回来的数据是id的一个数组，将业务交给service'
        try {
            petService.onSale(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage()+"操作失败");
        }

    }
    @PatchMapping("/offSale")
    public AjaxResult offSale(@RequestBody Long[] ids){
        // 传回来的数据是id的一个数组，将业务交给service'
        try {
            petService.offSale(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage()+"操作失败");
        }

    }














}
